package m2;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //连接消息服务器
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140");
        f.setUsername("admin");
        f.setPassword("admin");
        Channel c = f.newConnection().createChannel();

        //定义队列
        c.queueDeclare("task_queue",true,false,false,null);


        DeliverCallback deliverCallback = new DeliverCallback() {
            @Override
            public void handle(String s, Delivery delivery) throws IOException {
                byte[] body = delivery.getBody();
                String msg = new String(body);
                System.out.println("收到："+msg);
                for (int i=0;i<msg.length();i++){
                    if (msg.charAt(i)=='.'){
                        try {
                            Thread.sleep(1000);
                        }catch (InterruptedException e){

                        }
                    }
                }
                System.out.println("------------消息处理结束\n\n");
            }
        };
        CancelCallback cancelCallback = new CancelCallback() {

            @Override
            public void handle(String s) throws IOException {

            }
        };
        //
        c.basicQos(1);
        //从hellworld队列接收消息 消费数据
        //第二个参数是ack模式
        //true-自动确认
        //false=手动确认
        c.basicConsume("task_queue",true,deliverCallback,cancelCallback);

    }
}
